package com.outstanding.his.symptom.facade;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.outstanding.his.patient.domain.Patient;
import com.outstanding.his.patient.service.IPatientService;
import com.outstanding.his.symptom.domain.Basic;
import com.outstanding.his.symptom.domain.Condition;
import com.outstanding.his.symptom.domain.Disease;
import com.outstanding.his.symptom.domain.Excreta;
import com.outstanding.his.symptom.domain.Eye;
import com.outstanding.his.symptom.domain.Habit;
import com.outstanding.his.symptom.domain.Head;
import com.outstanding.his.symptom.domain.Limb;
import com.outstanding.his.symptom.domain.Symptom;
import com.outstanding.his.symptom.domain.Tongue;
import com.outstanding.his.symptom.service.ISymptomService;

@Controller
@SessionAttributes("patient")
public class SymptomController {

	private ISymptomService symptomService;
	private IPatientService patientService;

	public void setSymptomService(ISymptomService symptomService) {
		this.symptomService = symptomService;
	}

	public void setPatientService(IPatientService patientService) {
		this.patientService = patientService;
	}

	@RequestMapping(value = "/symptom/new.do", method = RequestMethod.GET)
	public String symptomNewForm(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		return "/symptom/new";
	}

	@RequestMapping("/symptom/sumbit.do")
	public String symptomSubmit(@ModelAttribute("patient") Patient patient,
			@ModelAttribute("symptom") Symptom symptom,
			@ModelAttribute("basic") Basic basic,
			@ModelAttribute("condition") Condition condition,
			@ModelAttribute("habit") Habit habit,
			@ModelAttribute("head") Head head,
			@ModelAttribute("tongue") Tongue tongue,
			@ModelAttribute("eye") Eye eye, @ModelAttribute("limb") Limb limb,
			@ModelAttribute("excreta") Excreta excreta,
			@ModelAttribute("disease") Disease disease) {
		if (patient.getSymptom() != null) {
			symptom.setSymptomId(patient.getSymptom().getSymptomId());
		}
		// Patient patient = patientService.getPatient(patientId);
		// symptom.setPatient(patient);
		symptom.setBasic(basic);
		symptom.setCondition(condition);
		symptom.setHabit(habit);
		symptom.setHead(head);
		symptom.setTongue(tongue);
		symptom.setEye(eye);
		symptom.setLimb(limb);
		symptom.setExcreta(excreta);
		symptom.setDisease(disease);
		patient.setSymptom(symptom);
		symptom = symptomService.submitSymptom(symptom);
		patientService.updatePatient(patient);
		return "redirect:/symptom/view.do?patientId=" + patient.getPatientId();
	}

	@RequestMapping("/symptom/view.do")
	public String symptomView(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		// Symptom symptom = patient.getSymptom();
		// if (symptom != null) {
		// model.addAttribute(symptom);
		// return "redirect:/symptom/view?patientId" + patientId;
		// }
		return "/symptom/view";
	}

	@RequestMapping(value = "/symptom/edit.do", method = RequestMethod.GET)
	public String symptomEdit(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		return "/symptom/edit";
	}
	// @RequestMapping("/symptom/edit.do")
}